import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel
from PyQt6.QtGui import QIcon
from PyQt6.QtCore import Qt, QTimer
from batch_create_window import BatchCreateWindow
from batch_replace_window import BatchReplaceWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("蓝波文件批量处理工具")
        self.setWindowIcon(QIcon("icons\icon.ico"))  # 使用资源文件中的图标
        self.setGeometry(100, 100, 450, 350)

        # 创建中心部件和布局
        central_widget = QWidget()
        central_widget.setStyleSheet("""
            background-color: #f5f5f5;
        """)
        layout = QVBoxLayout(central_widget)
        layout.setSpacing(20)
        layout.setContentsMargins(30, 30, 30, 30)

        # 添加标题
        title_label = QLabel("蓝波文件批量处理工具")
        title_label.setStyleSheet("""
            font-size: 20px;
            font-weight: bold;
            color: #333;
            padding-bottom: 15px;
        """)
        title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(title_label)

        # 创建并添加批量创建按钮
        self.create_button = QPushButton("📁 批量创建")
        self.create_button.setFixedSize(250, 70)
        self.create_button.setStyleSheet("""
            QPushButton {
                background-color: #00B19D;
                color: white;
                font-size: 16px;
                font-weight: bold;
                border-radius: 5px;
            }
            QPushButton:hover {
                background-color: #009688;
            }
        """)
        self.create_button.clicked.connect(self.open_batch_create_window)
        layout.addWidget(self.create_button, alignment=Qt.AlignmentFlag.AlignCenter)

        # 添加批量创建描述
        create_label = QLabel("支持批量创建文件夹、WORD、EXCEL、PDF、PPT、txt等任意文件")
        create_label.setStyleSheet("color: #555;")
        create_label.setWordWrap(True)
        create_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(create_label)

        # 创建并添加批量替换按钮
        self.replace_button = QPushButton("🔄 批量替换")
        self.replace_button.setFixedSize(250, 70)
        self.replace_button.setStyleSheet("""
            QPushButton {
                background-color: #FF9F43;
                color: white;
                font-size: 16px;
                font-weight: bold;
                border-radius: 5px;
            }
            QPushButton:hover {
                background-color: #FF8F33;
            }
        """)
        self.replace_button.clicked.connect(self.open_batch_replace_window)
        layout.addWidget(self.replace_button, alignment=Qt.AlignmentFlag.AlignCenter)

        # 添加批量替换描述
        replace_label = QLabel("支持Excel数据快速写入WORD模板，批量创建一个或多个WORD文件")
        replace_label.setStyleSheet("color: #555;")
        replace_label.setWordWrap(True)
        replace_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(replace_label)

        # 添加底部版权信息
        footer_label = QLabel("© 2025 文件批量处理工具 v1.0")
        footer_label.setStyleSheet("""
            color: #888;
            font-size: 12px;
            padding-top: 20px;
        """)
        footer_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(footer_label)

        # 设置中心部件
        self.setCentralWidget(central_widget)

        # 初始化批量创建和批量替换窗口
        self.batch_create_window = None
        self.batch_replace_window = None

    def open_batch_create_window(self):
        # 每次都创建新的批量创建窗口
        if self.batch_create_window:
            self.batch_create_window.deleteLater()
        self.batch_create_window = BatchCreateWindow()
        self.batch_create_window.closed.connect(self.show_main_window)
        self.batch_create_window.setWindowFlags(Qt.WindowType.Window)  # 设置为独立窗口
        self.batch_create_window.show()
        self.batch_create_window.activateWindow()  # 激活窗口
        QTimer.singleShot(100, self.hide)  # 延迟隐藏主窗口

    def show_main_window(self):
        self.show()
        self.activateWindow()  # 激活主窗口
        if self.batch_create_window:
            self.batch_create_window.close()  # 关闭批量创建窗口
            self.batch_create_window = None
        if self.batch_replace_window:
            self.batch_replace_window.close()  # 关闭批量替换窗口
            self.batch_replace_window = None

    def open_batch_replace_window(self):
        # 每次都创建新的批量替换窗口
        if self.batch_replace_window:
            self.batch_replace_window.deleteLater()
        self.batch_replace_window = BatchReplaceWindow()
        self.batch_replace_window.closed.connect(self.show_main_window)
        self.batch_replace_window.setWindowFlags(Qt.WindowType.Window)  # 设置为独立窗口
        self.batch_replace_window.show()
        self.batch_replace_window.activateWindow()  # 激活窗口
        QTimer.singleShot(100, self.hide)  # 延迟隐藏主窗口

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())